#include"boxdemo.h"
#include"memory.h"
Boxgame::Boxgame()
{
    ininMap();
}
void Boxgame::ininMap()
{
    int graph[8][8]=
    {
        {1,1,1,1,1,1,1,1},
        {1,3,4,0,0,0,1,1},
        {1,0,1,1,1,0,0,1},
        {1,0,0,5,0,0,1,1},
        {1,0,1,0,1,0,1,1},
        {1,0,1,0,1,0,1,1},
        {1,0,0,0,4,3,0,1},
        {1,1,1,1,1,1,1,1}
    };
    memcpy(&map[0][0],&graph[0][0],sizeof(int)*64);
}
void Boxgame::drawmap(QPainter* pDraw)
{
    QString imgURL;
    for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {

                switch(map[i][j])
                {
                case 0:
                   imgURL=":/new/prefix1/D:/QT_boxgame/08.jpg.jpg";
                    break;
                case 1:
                    imgURL=":/new/prefix1/D:/QT_boxgame/03.jpg.jpg";
                    break;
                case 3:
                     imgURL=":/new/prefix1/D:/QT_boxgame/003.jpg";
                    break;
                case 4:
                     imgURL=":/new/prefix1/D:/QT_boxgame/002.jpg";
                    break;
                case 5:
                case 8:
                     imgURL=":/new/prefix1/D:/QT_boxgame/001.jpg";
                    break;
                case 7:
                  imgURL=":/new/prefix1/D:/QT_boxgame/09.jpg.jpeg";
                    break;
                }
                QImage img(imgURL);
                pDraw->drawImage(QRect(j*img.width(),i*img.height(),img.width(),img.height()),img);
            }

        }
}
void Boxgame::keydown(QKeyEvent* event)
{
        int i=getrow();
        int j=getcols();
        switch(event->key())
        {
       case Qt::Key_W:
        case Qt::Key_Up:
            if(map[i-1][j]==0||map[i-1][j]==3)
            {
                map[i][j]-=5;
                map[i-1][j]+=5;
            }
            if(map[i-1][j]==4||map[i-1][j]==7)
            {
                if(map[i-2][j]==0||map[i-2][j]==3)
                {
                    map[i][j]-=5;
                    map[i-1][j]+=1;
                    map[i-2][j]+=4;
                }
            }
            break;
        case Qt::Key_S:
         case Qt::Key_Down:
            if(map[i+1][j]==0||map[i+1][j]==3)
            {
                map[i][j]-=5;
                map[i+1][j]+=5;
            }
            if(map[i+1][j]==4||map[i+1][j]==7)
            {
                if(map[i+2][j]==0||map[i+2][j]==3)
                {
                    map[i][j]-=5;
                    map[i+1][j]+=1;
                    map[i+2][j]+=4;
                }
            }
            break;
        case Qt::Key_A:
         case Qt::Key_Left:
            if(map[i][j-1]==0||map[i][j-1]==3)
            {
                map[i][j]-=5;
                map[i][j-1]+=5;
            }
            if(map[i][j-1]==4||map[i][j-1]==7)
            {
                if(map[i][j-2]==0||map[i][j-2]==3)
                {
                    map[i][j]-=5;
                    map[i][j-1]+=1;
                    map[i][j-2]+=4;
                }
            }
            break;
        case Qt::Key_D:
         case Qt::Key_Right:
            if(map[i][j+1]==0||map[i][j+1]==3)
            {
                map[i][j]-=5;
                map[i][j+1]+=5;
            }
            if(map[i][j+1]==4||map[i][j+1]==7)
            {
                if(map[i][j+2]==0||map[i][j+2]==3)
                {
                    map[i][j]-=5;
                    map[i][j+1]+=1;
                    map[i][j+2]+=4;
                }
            }
            break;
    }
}
int Boxgame::getrow()
{
    for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                if(map[i][j]==5||map[i][j]==8)
                    return i;
            }
        }
        return -1;
}
int Boxgame::getcols()
{
    for(int i=0;i<8;i++)
        {
            for(int j=0;j<8;j++)
            {
                if(map[i][j]==5||map[i][j]==8)
                    return j;
            }
        }
        return -1;
}
bool Boxgame::gameover()
{
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            if(map[i][j]==4)
            {
                return false;
            }
        }
    }
    return true;
}
